/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doda2;

import com.sun.messaging.ConnectionConfiguration;
import javax.jms.Destination;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.MessageConsumer;
import javax.jms.JMSException;
import java.io.InputStreamReader;
import java.io.IOException;
/**
 *
 * @author Shadownest
 */
public class ServiceOperatorClient
{
    private Connection connection;
    private MessageConsumer consumer;
    private Session session;
    
    public static void main(String[] args)
    {
        ServiceOperatorClient cons = new ServiceOperatorClient();
        cons.receiveMessages();
    }
    
    public ServiceOperatorClient()
    {
        setupConnection();
    }
    
    private void setupConnection()
    {
        com.sun.messaging.ConnectionFactory connectionFactory = null;
        Destination dest = null;
        final String host = "localhost";
        try
        {
            connectionFactory = new com.sun.messaging.ConnectionFactory();
            connectionFactory.setProperty(ConnectionConfiguration.imqAddressList, host + ":7676");
        }
        catch (JMSException e)
        {
            System.err.println("Error creating connection factory: " + e.toString());
            System.exit(1);
        }
        try
        {
            dest = (Destination) new com.sun.messaging.Queue("grp02OperatorQueue");
        }
        catch (JMSException e)
        {
            System.err.println("Error creating queue/topic or setting destination: " + e.toString());
            System.exit(1);
        }
        try
        {
            connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            consumer = session.createConsumer(dest);
        }
        catch (JMSException e)
        {
            System.err.println("Error creating connection, session or consumer: " + e.toString());
        }
    }
    
    public void receiveMessages()
    {
        try
        {
            ServiceOperatorListener listener = new ServiceOperatorListener();
            consumer.setMessageListener(listener);
            connection.start();
            System.out.println("To end program, type Q or q, " + "then <return>");
            InputStreamReader inputStreamReader = new InputStreamReader(System.in);
            char answer = '\0';
            while (!((answer == 'q') || (answer == 'Q')))
            {
                try
                {
                    answer = (char) inputStreamReader.read();
                }
                catch (IOException e)
                {
                    System.err.println("I/O exception: " + e.toString());
                }
            }
        }
        catch (JMSException e)
        {
            System.err.println("JMS exception occurred: " + e.toString());
        }
        finally
        {
            if (connection != null)
            {
                try
                {
                    connection.close();  // Finally, close connection
                }
                catch (JMSException e)
                {
                }
            }
        }
    }
}
